<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="140px">
    <el-form-item label="发布人编号" prop="publishNo">
      <el-input v-model="dataForm.publishNo" placeholder="发布人编号"></el-input>
    </el-form-item>
    <el-form-item label="发布人姓名" prop="publishName">
      <el-input v-model="dataForm.publishName" placeholder="发布人姓名"></el-input>
    </el-form-item>
    <el-form-item label="消息内容" prop="publishContent">
      <el-input v-model="dataForm.publishContent" placeholder="消息内容"></el-input>
    </el-form-item>
    <el-form-item label="接收人编号" prop="receiveNo">
      <el-input v-model="dataForm.receiveNo" placeholder="接收人编号"></el-input>
    </el-form-item>
    <el-form-item label="接收人姓名" prop="receiveName">
      <el-input v-model="dataForm.receiveName" placeholder="接收人姓名"></el-input>
    </el-form-item>
    <el-form-item label="发布时间" prop="publishTime">
      <el-input v-model="dataForm.publishTime" placeholder="发布时间"></el-input>
    </el-form-item>
    <el-form-item label="消息状态" prop="checkResult">
      <el-input v-model="dataForm.checkResult" placeholder="消息状态(未确认/已确认)"></el-input>
    </el-form-item>
    <el-form-item label="消息类别" prop="messageType">
      <el-input v-model="dataForm.messageType" placeholder="消息类别"></el-input>
    </el-form-item>
    <el-form-item label="消息批注" prop="messageContent">
      <el-input v-model="dataForm.messageContent" placeholder="消息批注"></el-input>
    </el-form-item>
    <el-form-item label="考试成绩" prop="examGrade">
      <el-input v-model="dataForm.examGrade" placeholder="考试成绩"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          majorName: '',
          gmtCreate: '',
          createBy: '',
          gmtModified: '',
          lastModifiedBy: '',
          isDeleted: '',
          sortNo: '',
          version: '',
          publishNo: '',
          publishName: '',
          publishContent: '',
          receiveNo: '',
          receiveName: '',
          publishTime: '',
          checkResult: '',
          messageType: '',
          messageContent: '',
          examGrade: ''
        },
        dataRule: {
          majorName: [
            { required: true, message: '专业名不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '记录创建者不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
          ],
          lastModifiedBy: [
            { required: true, message: '记录最后更新人不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除(1删除,0未删除)不能为空', trigger: 'blur' }
          ],
          sortNo: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本不能为空', trigger: 'blur' }
          ],
          publishNo: [
            { required: true, message: '发布人编号不能为空', trigger: 'blur' }
          ],
          publishName: [
            { required: true, message: '发布人姓名不能为空', trigger: 'blur' }
          ],
          publishContent: [
            { required: true, message: '消息内容不能为空', trigger: 'blur' }
          ],
          receiveNo: [
            { required: true, message: '接收人编号不能为空', trigger: 'blur' }
          ],
          receiveName: [
            { required: true, message: '接收人姓名不能为空', trigger: 'blur' }
          ],
          publishTime: [
            { required: true, message: '发布时间不能为空', trigger: 'blur' }
          ],
          checkResult: [
            { required: true, message: '消息状态不能为空', trigger: 'blur' }
          ],
          messageType: [
            { required: true, message: '消息类别不能为空', trigger: 'blur' }
          ],
          messageContent: [
            { required: true, message: '消息批注不能为空', trigger: 'blur' }
          ],
          examGrade: [
            { required: true, message: '考试成绩不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/notice/tmessagenotice/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.majorName = data.tMessageNotice.majorName
                this.dataForm.gmtCreate = data.tMessageNotice.gmtCreate
                this.dataForm.createBy = data.tMessageNotice.createBy
                this.dataForm.gmtModified = data.tMessageNotice.gmtModified
                this.dataForm.lastModifiedBy = data.tMessageNotice.lastModifiedBy
                this.dataForm.isDeleted = data.tMessageNotice.isDeleted
                this.dataForm.sortNo = data.tMessageNotice.sortNo
                this.dataForm.version = data.tMessageNotice.version
                this.dataForm.publishNo = data.tMessageNotice.publishNo
                this.dataForm.publishName = data.tMessageNotice.publishName
                this.dataForm.publishContent = data.tMessageNotice.publishContent
                this.dataForm.receiveNo = data.tMessageNotice.receiveNo
                this.dataForm.receiveName = data.tMessageNotice.receiveName
                this.dataForm.publishTime = data.tMessageNotice.publishTime
                this.dataForm.checkResult = data.tMessageNotice.checkResult
                this.dataForm.messageType = data.tMessageNotice.messageType
                this.dataForm.messageContent = data.tMessageNotice.messageContent
                this.dataForm.examGrade = data.tMessageNotice.examGrade
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/notice/tmessagenotice/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'majorName': this.dataForm.majorName,
                'gmtCreate': this.dataForm.gmtCreate,
                'createBy': this.dataForm.createBy,
                'gmtModified': this.dataForm.gmtModified,
                'lastModifiedBy': this.dataForm.lastModifiedBy,
                'isDeleted': this.dataForm.isDeleted,
                'sortNo': this.dataForm.sortNo,
                'version': this.dataForm.version,
                'publishNo': this.dataForm.publishNo,
                'publishName': this.dataForm.publishName,
                'publishContent': this.dataForm.publishContent,
                'receiveNo': this.dataForm.receiveNo,
                'receiveName': this.dataForm.receiveName,
                'publishTime': this.dataForm.publishTime,
                'checkResult': this.dataForm.checkResult,
                'messageType': this.dataForm.messageType,
                'messageContent': this.dataForm.messageContent,
                'examGrade': this.dataForm.examGrade
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
